﻿using System;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;

namespace Interscape.TwilioClient
{
    /// <summary>
    /// Represents an Exception thrown from TwilioClient. Any errors returned from the TwilioAPI will throw this exception.
    /// </summary>
    [Serializable]
    public class TwilioClientException : Exception
    {

        /// <summary>
        /// Creates a new instance of the TwilioClientException object.
        /// </summary>
        public TwilioClientException()
        {
        }

        /// <summary>
        /// Creates a new instance of the TwilioClientException object.
        /// </summary>
        /// <param name="message"></param>
        public TwilioClientException(string message)
            : base(message)
        { 
        }

        /// <summary>
        /// Creates a new instance of the TwilioClientException object.
        /// </summary>
        /// <param name="re"></param>
        public TwilioClientException(TwilioRestError re)
        {
            this.RestException = re;
        }

        /// <summary>
        /// Creates a new instance of the TwilioClientException object.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="re"></param>
        public TwilioClientException(string message, TwilioRestError re)
            : base(message)
        {
            this.RestException = re;
        }

        /// <summary>
        /// Creates a new instance of the TwilioClientException object.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="re"></param>
        /// <param name="xdoc"></param>
        public TwilioClientException(string message, TwilioRestError re, XmlDocument xdoc)
            : base(message)
        {
            this.RestException = re;
            this.PostedObject = xdoc;
        }

        /// <summary>
        /// Creates a new instance of the TwilioClientException object.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="inner"></param>
        public TwilioClientException(string message, Exception inner)
            : base(message, inner)
        { 
        }

        /// <summary>
        /// Creates a new instance of the TwilioClientException object.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        /// <remarks>This constructor is needed for serialization.</remarks>
        protected TwilioClientException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// The TwilioRestError returned from the TwilioAPI. 
        /// </summary>
        [XmlElement]
        public TwilioRestError RestException { get; set; }

        /// <summary>
        /// The object posted to the TwilioAPI.
        /// </summary>
        [XmlElement]
        public XmlDocument PostedObject { get; set; }

    }
}
